package com.tianpengtech.common.validator.rules.string;

import org.apache.commons.lang.StringUtils;

import com.tianpengtech.common.exception.ValidateException;
import com.tianpengtech.common.validator.AbstractRule;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by tianpengtech on 16/8/18.
 */
public class StringMustBeDateYYYYDDMM extends AbstractRule {
    private String rex = "^((\\\\d{2}(([02468][048])|([13579][26]))[\\\\-\\\\-\\\\s]?((((0?\" +\"[13578])|(1[02]))[\\\\-\\\\-\\\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))\" +\"|(((0?[469])|(11))[\\\\-\\\\-\\\\s]?((0?[1-9])|([1-2][0-9])|(30)))|\" +\"(0?2[\\\\-\\\\-\\\\s]?((0?[1-9])|([1-2][0-9])))))|(\\\\d{2}(([02468][12\" +\"35679])|([13579][01345789]))[\\\\-\\\\-\\\\s]?((((0?[13578])|(1[02]))\" +\"[\\\\-\\\\-\\\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))\" +\"[\\\\-\\\\-\\\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\\\-\\\\-\\\\s]?((0?[\" +\"1-9])|(1[0-9])|(2[0-8]))))))";

    public StringMustBeDateYYYYDDMM(String v, String rex, String msg) {
        this.value = v;
        this.msg = msg;
        this.rex = rex;
    }

    @Override
    public boolean validate() throws ValidateException {
        String v = StringUtils.trim((String) this.value);
        Pattern pattern = Pattern.compile(this.rex);
        Matcher matcher = pattern.matcher(v);
        if (!matcher.matches()) {
            throw new ValidateException(this.msg);
        }
        return true;
    }
}
